﻿using CCWin;
using System;
using System.Data;
using System.Windows.Forms;

namespace DC.Views
{
    public partial class Form_BackFee : Skin_DevExpress
    {
        Utils.Database DB = Utils.Global.GlobalDB;
        public string UserID = string.Empty;
        public string PatientID = string.Empty;
        public double debt = 0;


        public Form_BackFee()
        {
            InitializeComponent();
        }

        private void Form_BackFee_Load(object sender, EventArgs e)
        {
            loadinfo();
            loadUser();
        }

        private void loadinfo()
        {
            Name_label.Text = DB.GetData("select Patient_Name FROM Patient WHERE Patient_ID=" + PatientID).ToString();
            debt_label.Text = debt.ToString();
            backtime.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        private void loadUser()
        {
            //加载User下拉框
            comboBox1.Items.Clear();
            DataView UserList = DB.GetDs("select User_ID,Real_Name from [User]").Tables[0].DefaultView;
            comboBox1.DisplayMember = "Real_Name";
            comboBox1.ValueMember = "User_ID";
            comboBox1.DataSource = UserList;
            comboBox1.SelectedIndex = 0;
        }

        private void skinButton3_Click(object sender, EventArgs e)
        {
            DialogResult R = MessageBox.Show("确认要退款吗", "退款确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (textBox1.Text == string.Empty || Convert.ToSingle(textBox1.Text) < 0)
            {
                MessageBox.Show("退款金额有误", "出错", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if(comboBox1.Text == string.Empty)
            {
                MessageBox.Show("未选择操作员", "出错", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (R == DialogResult.Yes)
            {   //插入退款记录
                string sql = string.Format(@"insert into Fee(Fee_Time,Patient_ID,User_ID,Fee_Price,Fee_Should,Fee_Real,Fee_Debt,Fee_Payment,Fee_Status,Fee_Remark)
                                             values('{0}',{1},'{2}',{3},{4},{5},{6},'{7}','{8}','{9}')",
                                                    backtime.Text,
                                                    PatientID,
                                                    comboBox1.SelectedValue.ToString(),
                                                    0,
                                                    -Convert.ToSingle(textBox1.Text),
                                                    -Convert.ToSingle(skinLabel6.Text),
                                                    0,
                                                    string.Empty,
                                                    -3,
                                                    Reason_textbox.Text
                                                        );
                //修改欠款
                double finaldebt = 0;
                if (Convert.ToSingle(textBox1.Text) < Convert.ToSingle(debt_label.Text))
                {
                    finaldebt = Convert.ToSingle(debt_label.Text) - Convert.ToSingle(textBox1.Text);
                }
                string sql2 = @"update Patient set Patient_Debt=" + finaldebt + " WHERE Patient_ID=" + PatientID;
                DB.Execute(sql);
                DB.Execute(sql2);
                Close();
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == string.Empty)
            {
                return;
            }
            double realback = Convert.ToSingle(textBox1.Text) - debt;
            if (realback < 0)
            {
                realback = 0;
            }
            skinLabel6.Text = realback.ToString();
        }
    }
}
